import 'package:demo/routers/navigators.dart';
import 'package:flutter/material.dart';

import 'home_router.dart';

class NavigatorPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _NavigatorPageState();
  }
}

class _NavigatorPageState extends State<NavigatorPage> {
  int _selectedIndex = 0; //当前选中的索引
  final titles = ['首页', '运动', '发现', '我的'];

  Future<bool> onBack() {
    return Future.value(true);
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () {
        //退出页面
        return onBack();
      },
      child: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: new Text(titles[_selectedIndex]),
          elevation: 0,
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.search),
              onPressed: () {
                Navigators.push(context, HomeRouter.searchPage);
              },
            ),
          ],
        ),
        drawer: _selectedIndex == 0 ? Drawer() : null,
        body: IndexedStack(
          children: <Widget>[
            new Text(titles[0]),
            new Text(titles[1]),
            new Text(titles[2]),
            new Text(titles[3]),
          ],
          index: _selectedIndex, //选中的item
        ),
        bottomNavigationBar: BottomNavigationBar(
          items: <BottomNavigationBarItem>[
            BottomNavigationBarItem(
                icon: Icon(Icons.home), title: new Text(titles[0])),
            BottomNavigationBarItem(
                icon: Icon(Icons.assignment), title: new Text(titles[1])),
            BottomNavigationBarItem(
                icon: Icon(Icons.chat), title: new Text(titles[2])),
            BottomNavigationBarItem(
                icon: Icon(Icons.navigation), title: new Text(titles[3])),
          ],
          type: BottomNavigationBarType.fixed, //设置显示的模式
          currentIndex: _selectedIndex, //当前的item
          onTap: (index) {
            //点击底部tab item
            setState(() {
              _selectedIndex = index;
            });
          },
        ),
      ),
    );
  }
}
